		TITLE	OPEN_OUT - Copyright (C) SLR Systems 1994

		INCLUDE	MACROS
		INCLUDE	IO_STRUC
		INCLUDE	WIN32DEF

		PUBLIC	OPEN_OUTPUT,GET_OUTPUT_DEVICE


		.DATA

		EXTERNDEF	SHARE_ANDER:BYTE,ASCIZ:BYTE

		EXTERNDEF	_FILE_LIST_GARRAY:STD_PTR_S,MYO_STUFF:MYO_STRUCT


		.CODE	PASS2_TEXT

		EXTERNDEF	MOVE_FN_TO_ASCIZ:PROC,_recover_handle:PROC,_err_file_list_abort:proc
		EXTERNDEF	_FLUSH_DISABLE_MAPOUT:PROC,REPORT_OUT_ASCIZ:PROC

		EXTERNDEF	CANT_CREATE_ERR:ABS


GET_OUTPUT_DEVICE	PROC
		;
		;RETURN EAX AS NEXT AVAILABLE MYO_STUFF
		;
		MOV	EAX,OFF MYO_STUFF
		PUSH	EDI
L1$:
		MOV	ECX,[EAX].MYO_STRUCT.MYO_BUSY
		ADD	EAX,SIZE MYO_STRUCT

		TEST	ECX,ECX
		JNZ	L1$
L2$:
		SUB	EAX,SIZE MYO_STRUCT
		MOV	ECX,SIZE MYO_STRUCT/4

		MOV	EDI,EAX
		MOV	EDX,EAX

		XOR	EAX,EAX

		REP	STOSD

		DEC	ECX
		MOV	EAX,EDX

		POP	EDI
		MOV	[EDX].MYO_STRUCT.MYO_BUSY,ECX

		RET

GET_OUTPUT_DEVICE	ENDP


OPEN_OUTPUT	PROC
		;
		;EAX IS FILE_LIST_GINDEX
		;
		;RETURNS EAX = MYO_STRUCT
		;
		PUSHM	EDI,ESI

		MOV	EDI,EAX
		CALL	GET_OUTPUT_DEVICE		;GET AVAILABLE DEVICE

		PUSH	EBX
		MOV	EBX,EAX

		MOV	[EAX].MYO_STRUCT.MYO_FILE_LIST_GINDEX,EDI
		ASSUME	EBX:PTR MYO_STRUCT

		CONVERT	EDI,EDI,_FILE_LIST_GARRAY
		ASSUME	EDI:PTR FILE_LIST_STRUCT
L12$:
		MOV	EAX,FILE_FLAG_RANDOM_ACCESS
		MOV	CL,[EDI].FILE_LIST_FLAGS

		AND	CL,MASK FLF_RANDOM
		JNZ	L13$

		MOV	EAX,FILE_FLAG_SEQUENTIAL_SCAN

L13$:
		PUSH	0			;TEMPLATE FILE
		PUSH	EAX			;ATTRS&FLAGS

		MOV	EAX,OPEN_ALWAYS
		GETT	CL,FORCE_CREATE

		OR	CL,CL
		JZ	L14$

		MOV	EAX,CREATE_NEW
L14$:
		PUSH	EAX			;CREATE FLAGS
		PUSH	0			;SECURITY DESCRIPTOR

		PUSH	0			;DENY ALL
		PUSH	GENERIC_WRITE		;ACCESS MODE

		LEA	EDX,[EDI].FILE_LIST_NFN.NFN_TEXT

		PUSH	EDX
		CALL	CreateFile

		CMP	EAX,INVALID_HANDLE_VALUE
		JNZ	L3$

		CALL	_recover_handle
		TEST	EAX,EAX
		JZ	L12$

		JMP	CANT_CREATE

L3$:
L31$:
		MOV	[EBX].MYO_HANDLE,EAX
if	fgh_win32dll
		LEA	EAX,[EDI].FILE_LIST_NFN.NFN_TEXT
		CALL	REPORT_OUT_ASCIZ
endif
		MOV	EAX,EBX

		POPM	EBX,ESI,EDI

		RET

CANT_CREATE:
		CALL	_FLUSH_DISABLE_MAPOUT		;FLUSH AND STOP MAP OUTPUT

		MOV	ECX,[EBX].MYO_FILE_LIST_GINDEX
		MOV	AL,CANT_CREATE_ERR

		push	ECX
		push	EAX
		call	_err_file_list_abort
		add	ESP,8

OPEN_OUTPUT	ENDP


		END

